﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using CapstoneWinPhone.Data;
using System.Linq;
using System.Globalization;

namespace CapstoneWinPhone.Data
{
    public class DataHolder
    {
        #region holders
        private readonly Random _rad = new Random();
        public static List<User> users {get; set;}
        public static User _currentUser
        {
            get;
            set;
        }
        public static ExpenseLog _currentExpenseLog
        {
            get
            {
                return (_currentUser._ExpenseLogs.Count > 0) ? _currentUser._ExpenseLogs.Last() : null;
            }
            set
            {
                _currentExpenseLog = value;
            }
        }
        public static WeeklyTimeStamps _currentWeeklyTimestamp
        {
            get {
                Calendar cal = new GregorianCalendar();
                int weeknumber = cal.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
                WeeklyTimeStamps thisWeek = _currentUser._WeeklyTimeStamps.Where(wts => wts._WeekNumber == weeknumber).FirstOrDefault();
                if(thisWeek == null){
                    _currentUser._WeeklyTimeStamps.Add(new WeeklyTimeStamps(weeknumber));
                    thisWeek = _currentWeeklyTimestamp;
                }
                return thisWeek;
            }
        }
        public static MileageReport _currentMileageReport
        {
            get 
            { 
                return (_currentUser._MileageReports.Count > 0) ? _currentUser._MileageReports.Last() : null; 
            }
            set
            {
                _currentMileageReport = value;
            }
        }
        
        #endregion


        public DataHolder()
        {
        }

        #region userMethods
        
        public static void newUser(string username, string password, string email)
        {
            User newuser = new User(username, password, email);
            users.Add(newuser);
            _currentUser = newuser;
        }
        public static User getUser(string username, string password)
        {
            return users.Where(u => u._username == username && u._password == password).FirstOrDefault() ?? null;
        }
        #endregion

        #region MileageReportMethods

        public static void newMileageReport(string nameOfLog, string description)
        {
            MileageReport mr = new MileageReport(nameOfLog, description);
            _currentUser._MileageReports.Add(mr);
        }
        #endregion

        #region ExpenseLogMethods

        public static void newExpenseLog(string nameOfLog)
        {
            ExpenseLog el = new ExpenseLog(nameOfLog);
            _currentUser._ExpenseLogs.Add(el);
        }

        #endregion


    }
}
